﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace TCPServer
{
    class ClientThread
    {
        //connections变量表示连接数  
        public static int connections = 0;
        public Socket client;
        int i;
        public ClientThread(Socket clientsocket)
        {
            //service对象接管对消息的控制  
            this.client = clientsocket;
        }
        public void ClientService()
        {
            String data = null;
            byte[] bytes = new byte[1024];
            if (client != null)
            {
                connections++;
            }
            Console.WriteLine("新客户连接建立:{0} 个连接数", connections);
            while ((i = client.Receive(bytes)) != 0)
            {
                //data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

                MyHttpClient http = new MyHttpClient();
                http.Post(bytes);
                foreach (byte b in bytes)
                {
                    Console.Write("收到的数据: {0:X} ", b);
                }
                Console.Write("/n");
                // 处理客户端发来的消息，这里是转化为大写字母  
                data = data.ToUpper();
                byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                // 发送一条应答消息  
                //service.Send(msg);
                //Console.WriteLine("发送的数据: {0}", data);
            }
            //关闭套接字  
            client.Close();
            connections--;
            Console.WriteLine("客户关闭连接:{0} 个连接数", connections);
        }
    }  
}
